<template>
  <div :class="className" :id="id" :style="{ width, height }"></div>
</template>
  <script>
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts";
// 引入提示框，标题，直角坐标系组件，组件后缀都为 Component
import {
  DataZoomComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

import { LineChart } from "echarts/charts";
import { BarChart } from "echarts/charts";
import { PieChart } from "echarts/charts";
// 注册必须的组件
echarts.use([
  DataZoomComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  BarChart,
  CanvasRenderer,
  LineChart,
  PieChart,
  LegendComponent,
]);

export default {
  props: {
    className: {
      type: String,
      default: () => "classEcharts",
    },
    id: {
      type: String,
      default: () => "idEcharts",
    },
    width: {
      default: () => "100%",
    },
    height: {
      default: () => "100%",
    },
    options: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  watch: {
    options: {
      handler(val) {
        this.getCharts1();
      },
      // immediate: true,
      deep: true,
    },
  },
  mounted() {
    this.getCharts1();
    window.addEventListener("resize", () => {
      setTimeout(() => {
        this.myChart && this.myChart.resize();
        
      }, 200);
    });
  },
  methods: {
    resizeChart() {
      this.myChart && this.myChart.resize();
    },
    getCharts1() {
      if (this.myChart) {
        this.myChart.dispose();
      }
      this.myChart =
        document.getElementById(this.id) &&
        echarts.init(document.getElementById(this.id));
      this.myChart && this.myChart.off("legendselectchanged"); //解决重复触发
      this.myChart &&
        this.myChart.on("legendselectchanged", (e) => {
          this.$emit("legendClick", e);
        });
      this.myChart && this.options && this.myChart.setOption(this.options);
    },
  },
};
</script>
  